<?php
/**
 * 
 * @param unknown $firstname
 * @param unknown $lastname
 * @param unknown $dob
 * @param unknown $loginname
 * @param unknown $englishname
 * @param unknown $school
 * @param unknown $class
 * @param unknown $address
 * @param unknown $phonenumber
 * @param unknown $cover
 * @param unknown $note
 * @param unknown $learnningstatus
 * @param unknown $currentclass
 * @return Ambigous <number, WP_Error>
 */
function seitcm_add_student($firstname,$lastname,$dob,$loginname,$englishname,$school,$class,$address,$phonenumber,$cover,$note)
{
	$userdata = array (
			'ID' => '',
			'user_url' => '',
			'user_login' => $loginname,
			'user_email' => '',
			'first_name' => $firstname,
			'last_name' => $lastname,
			'user_pass' => $loginname,
			'role' => 'seitcm_student',
	);
	$id = wp_insert_user($userdata);
	add_user_meta($id, "englishname", $englishname);
	add_user_meta($id, "dob", $dob);
	add_user_meta($id, "school", $school);
	add_user_meta($id, "class", $class);
	add_user_meta($id, "address", $address);
	add_user_meta($id, "phonenumber", $phonenumber);
	add_user_meta($id, "cover", $cover);
	add_user_meta($id, "learningstatus", '1');
	add_user_meta($id, "currentclass", '0');
	return $id;
}
/**
 * 
 * @param unknown $id
 * @param unknown $firstname
 * @param unknown $lastname
 * @param unknown $dob
 * @param unknown $loginname
 * @param unknown $englishname
 * @param unknown $school
 * @param unknown $class
 * @param unknown $address
 * @param unknown $phonenumber
 * @param unknown $cover
 * @param unknown $note
 * @param unknown $learningstatus
 * @param unknown $currentclass
 * @return Ambigous <number, WP_Error>
 */
function seitcm_update_student($id,$firstname,$lastname,$dob,$loginname,$englishname,$school,$class,$address,$phonenumber,$cover,$note,$learningstatus,$currentclass)
{
	$userdata = array (
			'ID' => $id,
			'user_url' => '',
			'user_login' => $loginname,
			'user_email' => '',
			'first_name' => $firstname,
			'last_name' => $lastname,
			'user_pass' => $loginname,
			'role' => 'seitcm_student',
	);
	$id = wp_update_user($userdata);
	update_user_meta($id, "englishname", $englishname);
	update_user_meta($id, "dob", $dob);
	update_user_meta($id, "school", $school);
	update_user_meta($id, "class", $class);
	update_user_meta($id, "address", $address);
	update_user_meta($id, "phonenumber", $phonenumber);
	update_user_meta($id, "cover", $cover);
	update_user_meta($id, "learningstatus", $learningstatus);
	update_user_meta($id, "currentclass", $currentclass);
	return $id;
}
/**
 * 
 * @return Ambigous <multitype:, number>
 */
function seitcm_get_count_student()
{
	$args  = array(
			'role' => 'seitcm_student',
	);
	$wp_user_query = new WP_User_Query($args);
	return  $wp_user_query->get_total();
}
/**
 * 
 * @param number $limit
 * @param number $offset
 * @return multitype:
 */
function seitcm_get_list_student($number=10,$offset=0)
{
	$args  = array(
		'role' => 'seitcm_student',
			'number' => $number,
			'offset' => $offset,
			'orderby' => 'ID',
			'order' => 'DESC',
	);
	$wp_user_query = new WP_User_Query($args);
	return  $wp_user_query->get_results();
}
function seitcm_get_listcustom_student($classid,$status,$searchkey,$number,$offset)
{
	global $wpdb;
	if($classid!=-1 && $status!=0)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
						WHERE `meta_key` = \'learningstatus\'
							AND `meta_value` = '.$status.'
						        AND `user_id` IN(
									SELECT `user_id` FROM `wp_usermeta`
									WHERE `meta_key` = \'currentclass\'
										AND `meta_value` = '.$classid.'
							        		AND `user_id` IN (
							        			SELECT `user_id` FROM `wp_usermeta`
							                		WHERE `meta_key` LIKE \'wp_capabilities\'
							                		AND `meta_value` LIKE \'%seitcm_student%\'
							                	)
									)
						)';
	}
	else if($classid!=-1)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
							WHERE `meta_key` = \'currentclass\'
								AND `meta_value` = '.$classid.'
					        		AND `user_id` IN (
					        			SELECT `user_id` FROM `wp_usermeta`
					                		WHERE `meta_key` LIKE \'wp_capabilities\'
					                		AND `meta_value` LIKE \'%seitcm_student%\'
					                	)
						
						)';
	}
	else if($status!=0)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
							WHERE `meta_key` = \'learningstatus\'
								AND `meta_value` = '.$status.'
							        AND `user_id` IN(
					        			SELECT `user_id` FROM `wp_usermeta`
					                		WHERE `meta_key` LIKE \'wp_capabilities\'
					                		AND `meta_value` LIKE \'%seitcm_student%\'
					                	)
						)';
	}
	else {
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
	        			SELECT `user_id` FROM `wp_usermeta`
	                		WHERE `meta_key` LIKE \'wp_capabilities\'
	                		AND `meta_value` LIKE \'%seitcm_student%\'
						)';
	}
	$aid = array();
	foreach($wpdb->get_results($sql) as $id)
	{
		array_push($aid, $id->user_id);
	}
	if(count($aid)==0) return $aid;
	$wp_user_query = new WP_User_Query( array(
			'include' => $aid,
			'number' => $number,
			'offset' => $offset,
			'orderby' => 'ID',
			'order' => 'DESC',
			));
	return $wp_user_query->get_results();
}
function seitcm_get_countcustom_student($classid,$status,$searchkey)
{
	global $wpdb;
	if($classid!=-1 && $status!=0)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
						WHERE `meta_key` = \'learningstatus\'
							AND `meta_value` = '.$status.'
						        AND `user_id` IN(
									SELECT `user_id` FROM `wp_usermeta`
									WHERE `meta_key` = \'currentclass\'
										AND `meta_value` = '.$classid.'
							        		AND `user_id` IN (
							        			SELECT `user_id` FROM `wp_usermeta`
							                		WHERE `meta_key` LIKE \'wp_capabilities\'
							                		AND `meta_value` LIKE \'%seitcm_student%\'
							                	)
									)
						)';
	}
	else if($classid!=-1)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
							WHERE `meta_key` = \'currentclass\'
								AND `meta_value` = '.$classid.'
					        		AND `user_id` IN (
					        			SELECT `user_id` FROM `wp_usermeta`
					                		WHERE `meta_key` LIKE \'wp_capabilities\'
					                		AND `meta_value` LIKE \'%seitcm_student%\'
					                	)
						
						)';
	}
	else if($status!=0)
	{
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
						SELECT `user_id` FROM `wp_usermeta`
							WHERE `meta_key` = \'learningstatus\'
								AND `meta_value` = '.$status.'
							        AND `user_id` IN(
					        			SELECT `user_id` FROM `wp_usermeta`
					                		WHERE `meta_key` LIKE \'wp_capabilities\'
					                		AND `meta_value` LIKE \'%seitcm_student%\'
					                	)
						)';
	}
	else {
		$sql= 'SELECT `user_id` FROM `wp_usermeta`
			WHERE (`meta_key` = \'first_name\' OR `meta_key` = \'last_name\')
				AND `meta_value` LIKE \'%'.$searchkey.'%\'
			        AND `user_id` IN (
	        			SELECT `user_id` FROM `wp_usermeta`
	                		WHERE `meta_key` LIKE \'wp_capabilities\'
	                		AND `meta_value` LIKE \'%seitcm_student%\'
						)';
	}
	$aid = array();
	foreach($wpdb->get_results($sql) as $id)
	{
		array_push($aid, $id->user_id);
	}
	if(count($aid)==0) return 0;
	$wp_user_query = new WP_User_Query( array(
			'include' => $aid,
	));
	return $wp_user_query->get_total();
}
/**
 * 
 * @param unknown $user_ID
 * @return boolean|multitype:
 */
function seitcm_get_single_student($user_ID)
{
	$args  = array(
		'role' => 'seitcm_student',
			'search' => $user_ID,
			'search_columns' => array('ID')
	);
	$wp_user_query = new WP_User_Query($args);
	if($wp_user_query->get_total()!=1) return false;
	return array_shift($wp_user_query->get_results());
}
/**
 * 
 * @param unknown $user_ID
 * @return multitype:
 */
function seitcm_get_single_student_info($user_ID)
{
	$localdata = seitcm_get_single_student($user_ID);
	return array(
			'id' => $user_ID,
			'username' => $localdata->user_login,
			'firstname' => get_user_meta($user_ID,'first_name',true),
			'lastname' => get_user_meta($user_ID,'last_name',true),
			'dob' => get_user_meta($user_ID,'dob',true),
			'englishname' => get_user_meta($user_ID,'englishname',true),
			'school' => get_user_meta($user_ID,'school',true),
			'class' => get_user_meta($user_ID,'class',true),
			'address' => get_user_meta($user_ID,'address',true),
			'phonenumber' => get_user_meta($user_ID,'phonenumber',true),
			'cover' => get_user_meta($user_ID,'cover',true),
			'note' => get_user_meta($user_ID,'note',true),
			'currentclass' => get_user_meta($user_ID,'currentclass',true),
			'learningstatus' => get_user_meta($user_ID,'learningstatus',true),
	);
}
/**
 * 
 * @param unknown $user_ID
 * @return boolean
 */
function seitcm_check_user_is_student($user_ID)
{
	if(count(seitcm_get_single_student($user_ID))>0) return true;
	return false;
}
/**
 * 
 * @param int $user_ID
 * @return boolean
 */
function seitcm_check_exist_student_id($user_ID){
	foreach (seitcm_get_list_student(seitcm_get_count_student(),0) as $student)
	{
		if($student->ID == $user_ID)
		{
			return true;
			break;
		}
	}
	return false;
}
/**
 *  
 * @param int $studentid
 * @param int $classid
 * @param int $discount
 * @param boolean $collectedfee
 * @param string $time
 * @param string $note
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_register_class($studentid,$classid,$discount,$collectedfee,$time,$note)
{
	global $wpdb;
	$return = $wpdb->query($wpdb->prepare('INSERT INTO '. SEITCM_TABLE_REGISTER .' (studentid,classid,discounts,collectedfee,time,note) VALUES (%d,%d,%d,%d,%s,%s)',$studentid,$classid,$discount,$collectedfee,$time,$note));
	if($return)
	{
		$learningstatus = get_user_meta($studentid, 'learningstatus');
		if($learningstatus == 1 || $learningstatus == 4)
			update_user_meta($studentid, 'learningstatus', 2);
		else if($learningstatus == 3)
			update_user_meta($studentid, 'learningstatus', 4);
		return update_user_meta($studentid, 'currentclass', $classid);
	}
	return false;
}
/**
 * 
 * @param unknown $studentid
 * @param unknown $classid
 * @param unknown $time
 * @return Ambigous <string, NULL>
 */
function seitcm_find_register_class($studentid,$classid,$time)
{
	global $wpdb;
	return $wpdb->get_var($wpdb->prepare('SELECT id FROM ' . SEITCM_TABLE_REGISTER .' WHERE studentid = %d AND classid = %d AND time = %s',$studentid,$classid,$time));
}
/**
 * 
 * @param unknown $studentid
 * @param unknown $classid
 * @return Ambigous <string, NULL>
 */
function seitcm_check_student_registed_class($studentid,$classid)
{
	global $wpdb;
	return $wpdb->get_var($wpdb->prepare('SELECT COUNT(id) FROM '. SEITCM_TABLE_REGISTER .' WHERE studentid = %d AND classid = %d',$studentid,$classid));
}
/**
 * 
 * @param number $registerid
 * @return Ambigous <mixed, NULL, multitype:>
 */
function seitcm_get_single_register($registerid)
{
	global $wpdb;
	return $wpdb->get_row($wpdb->prepare('SELECT * FROM '. SEITCM_TABLE_REGISTER .' WHERE id = %d',$registerid));
}
/**
 * 
 * @param unknown $registerid
 * @return Ambigous <number, false, boolean, mixed>
 */
function seitcm_register_colletfee($registerid)
{
	global $wpdb;
	return $wpdb->query($wpdb->prepare('UPDATE '. SEITCM_TABLE_REGISTER .' WHERE id = %d',$registerid));
}
/**
 * 
 * @param string $loginname
 * @return number
 */
function seitcm_find_user_id($loginname)
{
	return get_user_by('login',$loginname)->ID;
}
?>